package com.dtborad.myblog.common;

import com.github.houbb.heaven.util.lang.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class MusicCrawlerUtil {
    public List<Map> getMusicList(String name) {
        String url = "https://dev.iw233.cn/Music1/";
        List<Map> musicList = new ArrayList<>();
        try {
            int currentIndex = 0;
            //循环爬取五次
            for(int k = 0; k < 5; k++){
                Document doc = Jsoup.connect(url)
                        // 设置请求头
                        .header("accept", "application/json, text/javascript, */*; q=0.01")
                        .header("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                        .header("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .header("sec-ch-ua", "\"Not(A:Brand\";v=\"99\", \"Microsoft Edge\";v=\"133\", \"Chromium\";v=\"133\"")
                        .header("sec-ch-ua-mobile", "?0")
                        .header("sec-ch-ua-platform", "\"Windows\"")
                        .header("sec-fetch-dest", "empty")
                        .header("sec-fetch-mode", "cors")
                        .header("sec-fetch-site", "same-origin")
                        .header("x-requested-with", "XMLHttpRequest")
                        .header("referrer", "https://dev.iw233.cn/Music1/?name=%E5%A4%9A%E4%BD%99%E7%9A%84%E8%A7%A3%E9%87%8A&type=netease")
                        .header("referrerPolicy", "strict-origin-when-cross-origin")
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0")
                        // 设置请求体
                        .data("input", name)
                        .data("filter", "name")
                        .data("type", "netease")
                        .data("page", k + "")
                        .ignoreContentType(true)
                        .post();
                String body = doc.body().toString().replaceAll("<body>", "").replaceAll("</body>", "");
                JSONObject json = new JSONObject(body);
                String code = json.optInt("code") + "";
                if ("200".equals(code)) {
                    //请求成功
                    JSONArray jsonArray = json.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String author = jsonObject.optString("author");//歌手
                        String pic = jsonObject.optString("pic");//封面
                        String title = jsonObject.optString("title");//歌名
                        String musicUrl = jsonObject.optString("url");//链接
                        String lrc = jsonObject.optString("lrc");//歌词
                        if(StringUtil.isBlank(musicUrl)){
                            continue;
                        }
                        Map<String, String> map = new HashMap<>();
                        map.put("id", currentIndex + "");
                        map.put("author", author);
                        map.put("pic", pic);
                        map.put("title", title);
                        map.put("url", musicUrl);
                        map.put("lrc", lrc);
                        musicList.add(map);
                        currentIndex++;
                    }
                }

                // 防止反爬机制 增加延迟
                Thread.sleep(1500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return musicList;
    }
}

